<template>
  <el-form :model="form" label-width="auto" style="max-width: 500px">
    <el-form-item label="类型：" style="display: flex; align-items: center;">
      <el-input
        v-model="form.type"
        style="width: 240px"
        placeholder="请输入"
        clearable
      />
      <el-button
        type="primary"
        :icon="Search"
        @click="PageDictionary()"
        style="margin-left: 10px;"
      >
        搜索
      </el-button>
      <el-button type="success" style="margin-left: 10px;">
        添加
      </el-button>
    </el-form-item>
  </el-form>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column prop="id" label="序号" />
    <el-table-column prop="type" label="类型" />
    <el-table-column prop="description" label="描述" />
    <el-table-column prop="mark" label="字典类型" />
    <el-table-column prop="remark" label="备注信息" />
    <el-table-column prop="createTime" label="创建时间" />
    <el-table-column prop="updateTime" label="修改时间" />
    <el-table-column label="操作">
      <template #default="scope">
        <el-button
          type="primary"
          :icon="Edit"
          circle
          @click="DictionaryUpdate(scope.row)"
        ></el-button>
        <el-button
          type="danger"
          :icon="Delete"
          circle
          @click="DictionaryDelete(scope.row.id)"
        ></el-button>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页 -->
  <div class="demo-pagination-block">
    <el-pagination
      v-model:current-page="form.pageNum"
      v-model:page-size="form.pageSize"
      :page-sizes="[5, 10, 15, 20]"
      :small="small"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
  <!--修改弹框-->
  <el-dialog v-model="dialogFormVisible" title="修改界面" width="500">
    <el-form :model="formData">
      <el-form-item label="序号">
        <el-input disabled v-model="formData.id" />
      </el-form-item>
      <el-form-item label="类型">
        <el-input v-model="formData.type" />
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="formData.description" />
      </el-form-item>
      <el-form-item label="字典类型">
        <el-input v-model="formData.mark" />
      </el-form-item>
      <el-form-item label="备注信息">
        <el-input v-model="formData.remark" />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-input disabled v-model="formData.createTime" />
      </el-form-item>
      <el-form-item label="修改时间">
        <el-input disabled v-model="formData.updateTime" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="dialogFormVisible = false">关闭</el-button>
        <el-button type="primary" @click="update()">
          修改
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script setup>
import { ref } from 'vue'
import request from '@/utils/request'
import { Delete, Edit, Search } from '@element-plus/icons-vue'
import { ElMessage } from 'element-plus'
const tableData = ref([])
// 分页 数据
const total = ref(10)
const small = ref(false)
const background = ref(false)
const disabled = ref(false)

const form = ref({
  pageNum: 1,
  pageSize: 5,
  type: '',
})
//分页方法
const handleCurrentChange = val => {
  form.value.pageNum = val
  PageDictionary()
}

const handleSizeChange = val => {
  form.value.pageSize = val
  PageDictionary()
}
function PageDictionary() {
  request({
    url: '/service-product/dictionary/list',
    method: 'get',
    params: form.value,
  }).then(res => {
    tableData.value = res.data.records
    total.value = res.data.total
  })
}
PageDictionary()

const dialogFormVisible = ref(false)

const formData = ref({
  id: '',
  type: '',
  description: '',
  mark: '',
  remark: '',
  createTime: '',
  updateTime: '',
})

function DictionaryUpdate(row) {
  dialogFormVisible.value = true
  formData.value.id = row.id
  formData.value.type = row.type
  formData.value.description = row.description
  formData.value.mark = row.mark
  formData.value.remark = row.remark
  formData.value.createTime = row.createTime
  formData.value.updateTime = row.updateTime
}

function update() {
  request({
    url: '/service-product/dictionary/update',
    method: 'put',
    params: formData.value,
  }).then(res => {
    if (res.code === 200) {
      ElMessage.success(res.msg)
      dialogFormVisible.value = false
      PageDictionary()
    } else {
      ElMessage.error(res.msg)
    }
  })
}

function DictionaryDelete(id) {
  var bo = confirm('确定删除吗？')
  if (bo) {
    request({
      url: '/service-product/dictionary/delete/',
      method: 'delete',
      params: {
        id: id,
      },
    }).then(res => {
      if (res.code === 200) {
        ElMessage.success(res.msg)
        PageDictionary()
      } else {
        ElMessage.error(res.msg)
      }
    })
  } else {
    ElMessage.info('你取消了删除操作')
  }
}
</script>
